package models

type ResCode int64

const (
	CodeSuccess ResCode = 1000 + iota
	CodeInvalidParam
	CodeUserExist
	CodeUserNotExist
	CodeInvalidPassword
	CodeServerBusy

	CodeNeedLogin
	CodeInvalidToken
	CodeCollectSuccess
	CodeSensitiveExist
	CodeHistoryNotFound   // 历史记录不存在
	CodeInvalidPermission // 权限不足
	// 新增收藏相关错误码
	CodePostNotExist      // 帖子不存在
	CodeAlreadyCollected  // 已收藏
	CodeNotCollected      // 未收藏
	CodeInvalidVoteStatus // 无效的投票状态
	CodeAlreadyVoted      // 已经投过票
	CodeCommunityNotFound // 没有这个社区id
	CodeInvalidOrderParam
)

var codeMsgMap = map[ResCode]string{
	CodeSuccess:         "success",
	CodeInvalidParam:    "请求参数错误",
	CodeUserExist:       "用户名已存在",
	CodeUserNotExist:    "用户名不存在",
	CodeInvalidPassword: "用户名或密码错误",
	CodeServerBusy:      "服务繁忙",

	CodeNeedLogin:         "需要登录",
	CodeInvalidToken:      "无效的token",
	CodeCollectSuccess:    "收藏成功",
	CodeSensitiveExist:    "含有敏感词",
	CodeHistoryNotFound:   "历史记录不存在",
	CodeInvalidPermission: "无效的权限",
	CodePostNotExist:      "帖子不存在",
	CodeAlreadyCollected:  "已收藏该帖子",
	CodeNotCollected:      "未收藏该帖子",
	CodeInvalidVoteStatus: "无效的投票状态",
	CodeAlreadyVoted:      "已经投过票",
	CodeCommunityNotFound: "没有这个社区id",
	CodeInvalidOrderParam: "没有这个排序关键字",
}

// Error 方法让 ResCode 实现了 error 接口
func (c ResCode) Error() string {
	return c.Msg()
}
func (c ResCode) Msg() string {
	msg, ok := codeMsgMap[c]
	if !ok {
		msg = codeMsgMap[CodeServerBusy]
	}
	return msg
}
